<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise封装ajax</title>
</head>
<body>
    
</body>
<script>
    //接口地址 ：https://api.apiopen.top/getJoke

    let p = new Promise(function(resolve,reject){
        //创建对象
        const xhr = new XMLHttpRequest();

        //初始化请求参数
        xhr.open('get',"https://api.apiopen.top/getJoke");

        //发送请求
        xhr.send();

        //绑定事件处理响应结果
        xhr.onreadystatechange = function(){
            //判断
            if(xhr.readyState===4){
                //判断响应状态码
                if(xhr.status>=200&&xhr.status<300){
                    resolve(xhr.response);
                }else{
                    //失败返回状态码
                    reject(xhr.status);
                }
            }
        }
    });

    p.then(function(value){
        console.log(value);
    },function(reason){
        console.log(reason);
    });


</script>
</html>